﻿<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="content-type" content="text/html;charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>

    <title></title>
    <link href="main.css" rel="stylesheet" type="text/css"/>

    <link href="test.css" rel="stylesheet" type="text/css"/>

    <style>
        .reading h2 {
            width: 100%;
            margin: 20px 0 70px;
            text-align: center;
            line-height: 2;
            font-size: 20px;
            color: #59595b;
        }

        .reading h2 a {
            text-decoration: none;
            color: #59595b;
            font-size: 20px;
        }

        .reading h2 a:hover {
            color: #2183f1;
        }
    </style>
</head>
<script src="jquery-1.11.3.min.js"></script>
<script src="jquery.countdown.js"></script>
<script>
    $(function () {
        var requestid = getUrlParam("requestid");
        var jsonArr = '';
        $.ajax({
            type: "post",
            async: false,
            //url: 'writedata.json',
            url: '/interface/xfjt/getWriteTest.jsp',
            dataType: 'json',
            data: {'requestid': requestid},
            success: function (data) {
                jsonArr = data;
            }
        });
        $('.test_name').text(jsonArr.examname);
        $('.test_datetime').text("考试期限:" + jsonArr.exambegdate + "至" + jsonArr.examenddate + "   考试时长:" + jsonArr.examtime+"(分钟)");
        $("#dxt_title").children().children().eq(1).text(jsonArr.dxttitle);
        $("#duoxt_title").children().children().eq(1).text(jsonArr.duoxttitle);
        $("#pdt_title").children().children().eq(1).text(jsonArr.pdttitle);
        $("#jdt_title").children().children().eq(1).text(jsonArr.jdttitle);
        var dxt_exam = '';
        if (jsonArr.dxt.length > 0) {
            jsonArr.dxt.map(function (item, index) {
                var options = '';
                item.child.map(function (lt, dx) {
                    var option =
                        "<li class='option'>" +
                        "<input type='radio' class='radioOrCheck' name='0_answer" + (index + 1) + "' id='0_answer_" + (index + 1) + "_option_" + (dx + 1) + "' value='" + lt.xxid + "'/>" +
                        "<label for='0_answer_" + (index + 1) + "_option_" + (dx + 1) + "'>" + lt.xxmc +
                        "<p class='ue' style='display: inline;'>" + lt.xxnr + "</p>" +
                        "</label>" +
                        "</li>";
                    options += option;
                })

                var exam_li_test_i = "<i>" + (index + 1) + "</i><span></span><font>" + item.tm + " (" + item.fz + ")分</font>";
                var exam_li_test = "<div class='test_content_nr_tt'>" + exam_li_test_i + "</div>";
                var exam_li_option_ul = "<ul>" + options + "</ul>";
                var exam_li_option = "<div class='test_content_nr_main'>" + exam_li_option_ul + "</div>";
                var exam_li = "<li id='qu_0_" + index + "'>" + exam_li_test + exam_li_option + " </li>";
                dxt_exam += exam_li;
            });
        } else {
            $("#dxt_title").hide();
        }
        $("#dxt_content").children().append(dxt_exam);
        jsonArr.dxt.map(function(item){
            if(item.da.xzdaid !=''){
                $("#qu_"+item.xh).find("input[type='radio']").eq((item.da.xzdaid-1)).attr("checked",true);
            }
        });
        var duozt_exam = '';
        if (jsonArr.duoxt.length > 0) {
            jsonArr.duoxt.map(function (item, index) {
                var options = '';
                item.child.map(function (lt, dx) {
                    var option =
                        "<li class='option'>" +
                        "<input type='checkbox' class='radioOrCheck' name='1_answer" + (index + 1) + "' id='1_answer_" + (index + 1) + "_option_" + (dx + 1) + "' value='" + lt.xxid + "'/>" +
                        "<label for='1_answer_" + (index + 1) + "_option_" + (dx + 1) + "'>" + lt.xxmc +
                        "<p class='ue' style='display: inline;'>" + lt.xxnr + "</p>" +
                        "</label>" +
                        "</li>";
                    options += option;
                })

                var exam_li_test_i = "<i>" + (index + 1) + "</i><span></span><font>" + item.tm + " (" + item.fz + ")分</font>";
                var exam_li_test = "<div class='test_content_nr_tt'>" + exam_li_test_i + "</div>";
                var exam_li_option_ul = "<ul>" + options + "</ul>";
                var exam_li_option = "<div class='test_content_nr_main'>" + exam_li_option_ul + "</div>";
                var exam_li = "<li id='qu_1_" + index + "'>" + exam_li_test + exam_li_option + " </li>";
                duozt_exam += exam_li;
            });
        } else {
            $("#duoxt_title").hide();
        }
        $("#duoxt_content").children().append(duozt_exam);
        jsonArr.duoxt.map(function(item){
            if(item.da.xzdaid !=''){
                var arr = item.da.xzdaid.split(",");
                for (var i = 0; i < arr.length; i++){
                    $("#qu_"+item.xh).find("input[type='checkbox']").eq(arr[i]-1).attr("checked",true);
                }
            }
        });
        var pdt_exam = '';
        if (jsonArr.pdt.length > 0) {
            jsonArr.pdt.map(function (item, index) {
                var options = '';
                item.child.map(function (lt, dx) {
                    var option =
                        "<li class='option'>" +
                        "<input type='radio' class='radioOrCheck' name='2_answer" + (index + 1) + "' id='2_answer_" + (index + 1) + "_option_" + (dx + 1) + "' value='" + lt.xxid + "'/>" +
                        "<label for='2_answer_" + (index + 1) + "_option_" + (dx + 1) + "'>" + lt.xxmc +
                        "</label>" +
                        "</li>";
                    options += option;
                })

                var exam_li_test_i = "<i>" + (index + 1) + "</i><span></span><font>" + item.tm + " (" + item.fz + ")分</font>";
                var exam_li_test = "<div class='test_content_nr_tt'>" + exam_li_test_i + "</div>";
                var exam_li_option_ul = "<ul>" + options + "</ul>";
                var exam_li_option = "<div class='test_content_nr_main'>" + exam_li_option_ul + "</div>";
                var exam_li = "<li id='qu_2_" + index + "'>" + exam_li_test + exam_li_option + " </li>";
                pdt_exam += exam_li;
            });
        } else {
            $("#pdt_title").hide();
        }
        $("#pdt_content").children().append(pdt_exam);
        jsonArr.pdt.map(function(item){
            if(item.da.xzdaid !=''){
                $("#qu_" + item.xh).find("input[type='radio']").eq((item.da.xzdaid - 1)).attr("checked", true);
            }
        });
        var jdt_exam = '';
        if (jsonArr.jdt.length > 0) {
            jsonArr.jdt.map(function (item, index) {
                var options = '';
                item.child.map(function (lt, dx) {
                    var option =
                        "<li>" +
                        "<textarea class='textarea' name='3_answer" + (index + 1) + "' id='3_answer_" + (index + 1) + "_option_" + (dx + 1) + "'></textarea>" +
                        "</li>";
                    options += option;
                })

                var exam_li_test_i = "<i>" + (index + 1) + "</i><span></span><font>" + item.tm + " (" + item.fz + ")分</font>";
                var exam_li_test = "<div class='test_content_nr_tt'>" + exam_li_test_i + "</div>";
                var exam_li_option_ul = "<ul>" + options + "</ul>";
                var exam_li_option = "<div class='test_content_nr_main'>" + exam_li_option_ul + "</div>";
                var exam_li = "<li id='qu_3_" + index + "'>" + exam_li_test + exam_li_option + " </li>";
                jdt_exam += exam_li;
            });
        } else {
            $("#jdt_title").hide();
        }
        $("#jdt_content").children().append(jdt_exam);
        jsonArr.jdt.map(function(item){
            $("#qu_"+item.xh).find('textarea').eq(0).val(item.da.xzdamc);
        });

        $("#savebtn").click(function () {
            var dxtarr = [];
            jsonArr.dxt.map(function (item) {
                var check_val = $("#qu_" + item.xh).find("input[type='radio']:checked").val();
                if (typeof (check_val) == "undefined") {
                    dxtarr.push({'xh': item.xh, 'txnr': ''})
                } else {
                    dxtarr.push({'xh': item.xh, 'txnr': check_val})
                }

            });

            var duoxtarr = [];
            jsonArr.duoxt.map(function (item, index) {
                var checkArr = [];
                var keytext = document.getElementsByName("1_answer" + (index + 1));
                if (typeof (keytext) == "undefined") {
                    duoxtarr.push({'xh': item.xh, 'txnr': ''})
                } else {
                    for (var i = 0; i < keytext.length; i++) {
                        var obj = keytext[i];
                        if (obj.type === "checkbox" && obj.checked) {
                            checkArr.push(obj.value);
                        }
                    }
                    duoxtarr.push({'xh': item.xh, 'txnr': checkArr.join()})
                }


            });

            var pdtarr = [];
            jsonArr.pdt.map(function (item) {
                var check_val = $("#qu_" + item.xh).find("input[type='radio']:checked").val();
                if (typeof (check_val) == "undefined") {
                    pdtarr.push({'xh': item.xh, 'txnr': ''})
                } else {
                    pdtarr.push({'xh': item.xh, 'txnr': check_val})
                }

            });

            var jdtarr = [];
            jsonArr.jdt.map(function (item) {
                var check_val = $("#qu_" + item.xh).find('div').eq(1).find('textarea').val();
                if (typeof (check_val) == "undefined") {
                    jdtarr.push({'xh': item.xh, 'txnr': ''})
                } else {
                    jdtarr.push({'xh': item.xh, 'txnr': check_val})
                }
            });

            var params = {'dxt': dxtarr, 'duoxt': duoxtarr, 'pdt': pdtarr, 'jdt': jdtarr};
            saveBtn(requestid, params);
        });

        $("#submitbtn").click(function () {
            var flag = hasSaveTest(requestid);
            var params = '';
            if(!flag){
                var dxtarr = [];
                jsonArr.dxt.map(function (item) {
                    var check_val = $("#qu_" + item.xh).find("input[type='radio']:checked").val();
                    if (typeof (check_val) == "undefined") {
                        dxtarr.push({'xh': item.xh, 'txnr': ''})
                    } else {
                        dxtarr.push({'xh': item.xh, 'txnr': check_val})
                    }

                });

                var duoxtarr = [];
                jsonArr.duoxt.map(function (item, index) {
                    var checkArr = [];
                    var keytext = document.getElementsByName("1_answer" + (index + 1));
                    if (typeof (keytext) == "undefined") {
                        duoxtarr.push({'xh': item.xh, 'txnr': ''})
                    } else {
                        for (var i = 0; i < keytext.length; i++) {
                            var obj = keytext[i];
                            if (obj.type === "checkbox" && obj.checked) {
                                checkArr.push(obj.value);
                            }
                        }
                        duoxtarr.push({'xh': item.xh, 'txnr': checkArr.join()})
                    }


                });

                var pdtarr = [];
                jsonArr.pdt.map(function (item) {
                    var check_val = $("#qu_" + item.xh).find("input[type='radio']:checked").val();
                    if (typeof (check_val) == "undefined") {
                        pdtarr.push({'xh': item.xh, 'txnr': ''})
                    } else {
                        pdtarr.push({'xh': item.xh, 'txnr': check_val})
                    }

                });

                var jdtarr = [];
                jsonArr.jdt.map(function (item) {
                    var check_val = $("#qu_" + item.xh).find('div').eq(1).find('textarea').val();
                    if (typeof (check_val) == "undefined") {
                        jdtarr.push({'xh': item.xh, 'txnr': ''})
                    } else {
                        jdtarr.push({'xh': item.xh, 'txnr': check_val})
                    }
                });
                params = {'dxt': dxtarr, 'duoxt': duoxtarr, 'pdt': pdtarr, 'jdt': jdtarr};
            }
            submitBtn(requestid, params);
        });
    });

    //保存按钮
    function saveBtn(requestid, params) {
        $.ajax({
            type: "post",
            async: false,
            url: "/interface/xfjt/saveTest_Ajax.jsp",
            dataType: 'json',
            data: {'requestid': requestid, 'result': JSON.stringify(params)},
            success: function (data) {
                if (data.status) {
                 window.close();
                 window.parent.WfForm.doRightBtnEvent("BTN_WFSAVE");
                }
            }
        });
    }
    //提交按钮
    function submitBtn(requestid, params) {
        $.ajax({
            type: "post",
            async: false,
            url: "/interface/xfjt/submitTest_Ajax.jsp",
            dataType: 'json',
            data: {'requestid': requestid, 'result': JSON.stringify(params)},
            success: function (data) {
                if (data.status) {
                    getKsdf(requestid);
                    window.close();
                    window.parent.WfForm.doRightBtnEvent("BTN_SUBMIT");
                }

            }
        });
    }
    //获取url地址参数
    function getUrlParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg);  //匹配目标参数
        if (r != null) return unescape(r[2]);
        return null; //返回参数值
    }
    //获取url地址参数
    function submitWf(requestid) {
        var datas = "";
        $.ajax({
            type: "post",
            async: false,
            url: "/interface/xfjt/submitWf_Ajax.jsp",
            dataType: 'json',
            data: {'requestid': requestid},
            success: function (data) {
                datas = data;

            }
        });
        return datas;
    }

    //判断是否已经保存试卷
    function hasSaveTest(requestid) {
        var flag = false;
        $.ajax({
            type: "get",
            async: false,
            url: "/interface/xfjt/hasSaveTest.jsp",
            dataType: 'json',
            data: {requestid: requestid},
            success: function (data) {
                flag = data.flag;
            }
        });
        return flag;
    }

    function getKsdf(requestid) {
        var flag = false;
        $.ajax({
            type: "get",
            async: false,
            url: "/interface/xfjt/getKsdf_Ajax.jsp",
            dataType: 'json',
            data: {requestid: requestid},
            success: function (data) {
                flag = data.flag;
            }
        });
        return flag;
    }
</script>


<body>
<div class="main">
    <div class="header"></div>
    <!--nr start-->
    <div class="test_main">
        <div class="nr_left">
            <div class="test">
                <form>
                    <!--标题-->
                    <div class="test_title">
                        <p class="test_name"></p>
                        <div class="test_datetime"></div>
                    </div>
                    <!--单选题-->
                    <div id="dxt_title" class="test_content">
                        <div class="test_content_title">
                            <h2>单选题</h2>
                            <p><span></span></p>
                        </div>
                    </div>
                    <div id="dxt_content" class="test_content_nr">
                        <ul></ul>
                    </div>
                    <!--多选题-->
                    <div id="duoxt_title" class="test_content">
                        <div class="test_content_title">
                            <h2>多选题</h2>
                            <p><span></span></p>
                        </div>
                    </div>
                    <div id="duoxt_content" class="test_content_nr">
                        <ul></ul>
                    </div>
                    <!--判断题-->
                    <div id="pdt_title" class="test_content">
                        <div class="test_content_title">
                            <h2>判断题</h2>
                            <p><span></span></p>
                        </div>
                    </div>
                    <div id="pdt_content" class="test_content_nr">
                        <ul></ul>
                    </div>
                    <!--简答题-->
                    <div id="jdt_title" class="test_content">
                        <div class="test_content_title">
                            <h2>简答题</h2>
                            <p><span></span></p>
                        </div>
                    </div>
                    <div id="jdt_content" class="test_content_nr">
                        <ul></ul>
                    </div>
                    <!--底部提交和保存按钮-->
                    <div class="test_foot">
                        <div class="btn">
                            <p class="submitbtn"><input type="button" id="submitbtn" value="提交">
                            <p class="savebtn"><input type="button" id="savebtn" value="保存">
                        </div>

                    </div>
                </form>
            </div>

        </div>

    </div>
    <!--nr end-->
    <div class="foot"></p>
    </div>
</div>
</body>
</html>

